package com.lwc.mq;

import com.lwc.mapper.Goodsmapper;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class Consumer {

    @Autowired
    Goodsmapper goodsmapper;

    @RabbitListener(queues = "normal-queue")
public  void  qiang(Message message, Channel channel){
        int num=goodsmapper.query(1);
        if (num>0){
 goodsmapper.update();
            //手动应答或拒绝    第一个参数消息的投递标记   第二个参数是否批量删除
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            System.out.println("订单已抢到");
        }else {
            System.out.println("订单已抢完");
        }


}


}
